package com.socool.sinofaith.dao;

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.socool.sinofaith.model.SifQuestionOptionsBean;
import com.socool.sinofaith.model.SifSubTaskBean;
import com.socool.sinofaith.util.AndroidSQLiteOpenHelper;

public class SifSubTaskDao {
	private AndroidSQLiteOpenHelper dbHelper;

	public SifSubTaskDao(Context context) {
		dbHelper = new AndroidSQLiteOpenHelper(context);
	}

	// 插入记录
	public int insert(SifSubTaskBean sifSubTaskBean) {
		Log.i("SQLite", "----insert----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"insert into "
							+ SifSubTaskBean.TABLENAME
							+ " (SubTask_ID,Task_ID,Template_ID,TemplateName,HasChild) values(?, ?, ?, ?, ?)",
					new Object[] { sifSubTaskBean.getSubTask_ID(),
							sifSubTaskBean.getTask_ID(),
							sifSubTaskBean.getTemplate_ID(),
							sifSubTaskBean.getTemplateName(),
							sifSubTaskBean.getHasChild() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 删除记录
	public int delete(SifSubTaskBean sifSubTaskBean) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifSubTaskBean.TABLENAME
					+ " where SubTask_ID = ?",
					new Object[] { sifSubTaskBean.getSubTask_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 根据taskId删除记录
	public int deleteByTaskId(String taskId) {
		Log.i("SQLite", "----delete----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL("delete from " + SifSubTaskBean.TABLENAME
					+ " where Task_ID = ?", new Object[] { taskId });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 更新记录
	public int update(SifSubTaskBean sifSubTaskBean) {
		Log.i("SQLite", "----update----");
		SQLiteDatabase db = dbHelper.getWritableDatabase();
		db.beginTransaction();
		try {
			db.execSQL(
					"update "
							+ SifSubTaskBean.TABLENAME
							+ " set Task_ID=?, Template_ID=?, TemplateName=?, HasChild=?,ModifyTime=? where SubTask_ID=?",
					new Object[] { sifSubTaskBean.getTask_ID(),
							sifSubTaskBean.getTemplate_ID(),
							sifSubTaskBean.getTemplateName(),
							sifSubTaskBean.getHasChild(),
							sifSubTaskBean.getModifyTime(),
							sifSubTaskBean.getSubTask_ID() });
			db.setTransactionSuccessful();
		} catch (Exception e) {
			return 0;
		} finally {
			db.endTransaction();
		}
		db.close();
		return 1;
	}

	// 查询记录
	public ArrayList<SifSubTaskBean> query(String id) {
		Log.i("SQLite", "----query----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifSubTaskBean sifSubTaskBean;
		ArrayList<SifSubTaskBean> list = new ArrayList<SifSubTaskBean>();
		// 若fileId为null或""则查询所有记录
		if (id == null || id.equals("")) {
			cursor = db.rawQuery("select * from " + SifSubTaskBean.TABLENAME,
					null);
		} else {
			cursor = db.rawQuery("select * from " + SifSubTaskBean.TABLENAME
					+ " where SubTask_ID=?", new String[] { id });
		}
		while (cursor.moveToNext()) {
			sifSubTaskBean = new SifSubTaskBean();
			sifSubTaskBean.setSubTask_ID(cursor.getString(cursor
					.getColumnIndex("SubTask_ID")));
			sifSubTaskBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifSubTaskBean.setTemplate_ID(cursor.getString(cursor
					.getColumnIndex("Template_ID")));
			sifSubTaskBean.setTemplateName(cursor.getString(cursor
					.getColumnIndex("TemplateName")));
			sifSubTaskBean.setHasChild(cursor.getString(cursor
					.getColumnIndex("HasChild")));
			sifSubTaskBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifSubTaskBean.toString());
			list.add(sifSubTaskBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}

	// 自定义查询记录
	public ArrayList<SifSubTaskBean> queryBySql(String sql) {
		Log.i("SQLite", "----queryBySql----");
		SQLiteDatabase db = dbHelper.getReadableDatabase();
		Cursor cursor;
		SifSubTaskBean sifSubTaskBean;
		ArrayList<SifSubTaskBean> list = new ArrayList<SifSubTaskBean>();
		cursor = db.rawQuery(sql, null);
		while (cursor.moveToNext()) {
			sifSubTaskBean = new SifSubTaskBean();
			sifSubTaskBean.setSubTask_ID(cursor.getString(cursor
					.getColumnIndex("SubTask_ID")));
			sifSubTaskBean.setTask_ID(cursor.getString(cursor
					.getColumnIndex("Task_ID")));
			sifSubTaskBean.setTemplate_ID(cursor.getString(cursor
					.getColumnIndex("Template_ID")));
			sifSubTaskBean.setTemplateName(cursor.getString(cursor
					.getColumnIndex("TemplateName")));
			sifSubTaskBean.setHasChild(cursor.getString(cursor
					.getColumnIndex("HasChild")));
			sifSubTaskBean.setModifyTime(cursor.getString(cursor
					.getColumnIndex("ModifyTime")));
			Log.i("SQLite", sifSubTaskBean.toString());
			list.add(sifSubTaskBean);
		}
		cursor.close();
		db.close();
		if (list.size() == 0) {
			Log.i("SQLite", "****表中无数据****");
		}
		return list;
	}
}
